// pages/memberAdd/memberAdd.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    tel:'',
    structureList:[],
    orgPositions:'',
    autoFocus:false,
    inOrg:true,
  },
  nameIpt(e) {
    this.toggle('name', e.detail.value)
  },
  telIpt(e) {
    this.toggle('tel', e.detail.value)
  },
  phoneBlur(e) {
    var phone = e.detail.value.replace(/ /g, '');
    if (!(/^((\d{3}-\d{8}|\d{4}-\d{7,8})|(1[3|4|5|6|7|8|9][0-9]{9}))$/.test(phone))) {
      wx.showToast({
        title: "手机号格式错误",
        icon: 'none',
        duration: 2000
      })
      return
    }
    this.checkWebOrgUser();
  },

  // 验证手机号
  checkWebOrgUser(){
    var that = this;
    var uid = wx.getStorageSync('uid');
    var phone = this.data.tel.replace(/ /g, '');
    app.req({
      url: "orgmini/org/newCheckWebOrgUser",
      method: "post",
      data: {
        uid: uid,
        orgId: that.data.orgId,
        applyTel: phone
      },
      success(res) {
        var text = '';
        if (res.code == 200) {
          if (res.data.type == 1) {
            text = '此用户已经是组织内成员';
          } else if (res.data.type == 2) {
            text = '此用户已被管理员冻结，请在官网恢复后再试';
          }else{
            that.toggle('inOrg', false)
          }
          if (text) {
            wx.showModal({
              title: '提示',
              content: text,
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  that.toggle('tel', "")
                  that.toggle('autoFocus', true)
                }
              }
            })
            return
          }
        }
      }
    })
  },
  orgPositionsIpt(e){
    this.toggle('orgPositions', e.detail.value)
  },
  validate: function () {
    var that = this;
    var text = "";
    var name = that.data.name;
    var phone = that.data.tel.replace(/ /g,'');
    var structure = that.data.structure
    if (!name) {
      text = "姓名不能为空";
    } else if (name.length > 10) {
      text = "姓名长度不能大于10";
    } else if (!phone) {
      text = "手机号码不能为空";
    } else if (!(/^1[3456789]\d{9}$/.test(phone))) {
      text = "手机号格式错误";
    }
    if (!!text) {
      wx.showToast({
        title: text,
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    return true;
  },
  onAuthClose: function () {
    this.toggle("showAuthList", false);
    this.toggle("authList", []);
    this.toggle("columns", []);
  },
  onAuthSelect: function (e) {
    var that = this;
    var i = e.detail.index
    var id = that.data.authList[i].id
    console.log(that.data.columns)
    var structure = that.data.columns[i]
    that.toggle('id',id)
    that.toggle('structure', structure)
    that.toggle("showAuthList", false);
  },
  toSave(){
    this.operation(this.data.id);
  },
  pass: function () {
    wx.navigateTo({
      url: '/pages/department/department?id=' + this.data.orgId
    })
    // this.toggle("authList", []);
    // this.toggle("columns", []);
    // var that = this;
    // app.req({
    //   url: "orgmini/org/getOrgStructure",
    //   data: {
    //     orgId: that.data.orgId
    //   },
    //   success: function (res) {
    //     if (200 == res.code) {
    //       var authList = [];
    //       var columns = [];
    //       for (var i of res.data.orgStructure) {
    //         authList.push({
    //           name: i.structure,
    //           id: i.id
    //         });
    //         columns.push(i.structure);
    //       }
    //       that.toggle("columns", columns);
    //       that.toggle("authList", authList);
    //       that.toggle("showAuthList", true);
    //     }
    //   }
    // });
  },
  operation: function ( authId) {
    var that = this;
    if (!that.validate()) {
      return;
    }
    var arr=[]
    for (var i in that.data.structureList){
      arr.push(that.data.structureList[i].id)
    }
    var param = that.data.param;
    var data = {};
    data.orgId = that.data.orgId;
    data.applyName = that.data.name;
    data.applyTel = that.data.tel.replace(/ /g, '');
    data.structureList = arr;
    data.orgPositions = that.data.orgPositions;
    if (!this.data.inOrg){
      app.req({
        url: "orgmini/org/newAddOrgUser",
        data: data,
        success: function (res) {
          if (200 == res.code) {
            wx.showToast({
              title: "添加成员成功",
              icon: 'none',
              duration: 2000
            })
            wx.navigateBack();
          }
        }
      });
    }
        
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.toggle('orgId', options.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})